let startY = 0;
let moveY = 0;
let moveDistance = 0;
import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: "translateY(0)",
    coverTransition: "",
    userInfo: {},
    recentRecord: [],
  },
  /* 
    methods
  */
  login() {
    console.log("login");
    // 跳转页面必须加根路径
    wx.navigateTo({
      url: "/pages/login/login",
    });
  },
  handleTouchStart(e) {
    startY = e.touches[0].clientY;
    this.setData({
      coverTransition: "",
    });
  },
  handleTouchMove(e) {
    moveY = e.touches[0].clientY;
    moveDistance = moveY - startY;
    if (parseInt(moveDistance) < 120 && parseInt(moveDistance) > 0) {
      this.setData({
        coverTransform: `translateY(${moveDistance}rpx)`,
      });
    }
  },
  handleTouchEnd(e) {
    this.setData({
      coverTransform: `translateY(0rpx)`,
      coverTransition: "all 0.5s",
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = wx.getStorageSync("userInfo");
    console.log(userInfo);
    if (userInfo) {
      this.setData({
        userInfo,
      });
      this.getUserRecentPlayList(userInfo.userId);
    }

    // const userRecordData = this.getUserRecentPlayList(userInfo.userId);
    // 注意不能再这里拿到值后再更新数据
    // 因为getUserRecentPlayList是一个异步函数不会阻塞进程
    // 下方更新数据时   userRecordData是undefined
    // this.setData({
    //   recentRecord: userRecordData,
    // });
  },
  async getUserRecentPlayList(userId) {
    const userRecordData = await request("/user/record", {
      uid: userId,
      type: 1,
    });
    const recentRecord = userRecordData.weekData.map((item, index) => {
      item.id = index++;
      return item;
    });
    this.setData({
      recentRecord: recentRecord.slice(0, 20),
    });
    // return userRecordData.weekData.slice(0, 20);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
